package org.paradoxfan.parsers

import util.parsing.combinator.RegexParsers
import util.parsing.input.CharSequenceReader

trait Vic2Parsers extends RegexParsers {

  implicit def str2charSeqReader(v:String) = new CharSequenceReader(v)

  lazy val key:Parser[String] = """[a-zA-Z_]*""".r

//  lazy val strValWithoutQuotes:Parser[String] = """(\p{L}+)+""".r
  lazy val letter:Parser[Char] = elem("Any letter", c=>(Character.isLetter(c)))
  lazy val strValWithoutQuotes:Parser[String] = rep1(letter) ^^ (_.mkString)

//  lazy val strValWithQuotes:Parser[String] = """(\"((\p{L}+)|\s)*\")""".r ^^ {
//    /* strip out the leading and trailing quotes */
//    v => v.slice(1, v.size-1)
//  }
  lazy val letterOrSpace:Parser[Char] = elem("Letter or whitespace", c=>(Character.isLetter(c) || c == ' '))
  lazy val strValWithQuotes:Parser[String] = elem('"') ~> rep(letterOrSpace) <~ "\""^^(_.mkString)



  lazy val integerVal:Parser[Int] = """[-]?\d+""".r ^^ { v => v.toInt }

  lazy val floatPointVal:Parser[Double] = """[-]?[0-9]+\.[0-9]+""".r ^^ { v => v.toDouble }

  lazy val numListVal:Parser[List[Double]] = elem('{') ~> rep(floatPointVal|(integerVal^^{xs=>xs.toDouble})) <~ '}'

  lazy val strListVal:Parser[List[String]] = elem('{') ~> rep(strValWithoutQuotes|strValWithQuotes) <~ '}'

//  lazy val entry:Parser[Pair[String,Any]] = (strValWithoutQuotes|strValWithQuotes) <~ '=' ~> (strValWithoutQuotes|strValWithQuotes|integerVal|floatPointVal|numListVal|strListVal) ^^ {
//    case k~v => (k, v)
//  }

//  lazy val entry = strValWithoutQuotes <~ '=' ~> strValWithoutQuotes ^^ {
//    case k ~ v => (k, v)
//  }
}